//
//  XinZeDiZhiViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/9/2.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "XinZeDiZhiViewController.h"

@interface XinZeDiZhiViewController ()

@end

@implementation XinZeDiZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"新增地址";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"dianchiliang"] forBarMetrics:UIBarMetricsDefault];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"duigou"] style:UIBarButtonItemStylePlain target:self action:@selector(BarQu)];
    
    
    self.view.backgroundColor = CL_efeff4;
    self.addBtn.backgroundColor = CL_00cc85;
    
   self.tel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"phoneNum"];
    
}

- (IBAction)sex:(UIButton *)sender{
    
    UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
    
    [sheet showInView:self.view];
    
    
}

- (IBAction)Addbutton:(UIButton *)sender{
    
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    
    if ([self.nameField.text isEqualToString:@""]||self.nameField == nil) {
        
        hud.mode = MBProgressHUDModeText;
        
        hud.labelText = @"请输入姓名";
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:1];
        
        return;
    }
    if ([self.ageField.text isEqualToString:@""]||self.ageField ==nil){
        
        hud.mode = MBProgressHUDModeText;
        
        hud.labelText = @"请输入年龄";
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:1];

        return;
    }
    if ([self.addressField.text isEqualToString:@""]||self.addressField ==nil){
        
        hud.mode = MBProgressHUDModeText;
        
        hud.labelText = @"请输入地址";
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:1];

        return;
    }
    if ([self.tel.text isEqualToString:@""]||self.tel ==nil){
        
        hud.mode = MBProgressHUDModeText;
        
        hud.labelText = @"请输入电话号码";
        
        hud.removeFromSuperViewOnHide = YES;
        
        [hud hide:YES afterDelay:1];
        
        return;
    }

    
    NSMutableDictionary * dicc =[[NSMutableDictionary alloc]initWithCapacity:0];
    
    [dicc setObject:self.nameField.text forKey:@"name"];
    
    [dicc setValue:self.sexBtn.titleLabel.text  forKey:@"sex"];
    
    [dicc setObject:@([self.ageField.text intValue]) forKey:@"age"];
    [dicc setObject:self.addressField.text forKey:@"addr"];
    long ID =[[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] longValue];
     [dicc setObject:self.tel.text forKey:@"tel"];
    
    [dicc setObject:@(ID) forKey:@"userId"];
    
    [QingQiuShuJu addDiZhi:dicc block:^(NSDictionary *dic, BOOL isOK) {
        
        NSLog(@"--%@",dic);
        
        if (isOK) {
            
            if ([[dic valueForKey:@"status"] intValue]==200) {
               
                self.block1([dic valueForKey:@"data"]);
                
                [self dismissViewControllerAnimated:YES completion:nil];

            }
            else{
                
            }
            
            [hud hide:YES];
            
        }
        else{
            [hud hide:YES];
        }
    }];
   
    
}
#pragma mark --- UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 2) {
        
        return;
    }
    
    NSLog(@"----%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
    [self.sexBtn setTitle:[actionSheet buttonTitleAtIndex:buttonIndex] forState:UIControlStateNormal];
    
    
}
#pragma mark --- UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    if (iphone4x_3_5) {
        if (textField.tag==2) {
            [UIView animateWithDuration:.5 animations:^{
                self.lableLayout.constant = -80.0f;
            }];

            
        }
        
        if (textField.tag ==1) {
            
            [UIView animateWithDuration:.5 animations:^{
                self.lableLayout.constant = -50.0f;
            }];

        }
        
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    if (textField.tag ==2||textField.tag ==1) {
        
        if (iphone4x_3_5) {
            
            [UIView animateWithDuration:.5 animations:^{
                self.lableLayout.constant = 29.0f;
            }];
            
        }
    }

    return YES;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
}
- (IBAction)addar:(UIButton *)sender{
    
    TheMap * th = [[TheMap alloc]init];
    __weak XinZeDiZhiViewController * weakSelf = self;
    
    th.block = ^(NSString * addr){
        
        weakSelf.addressField.text = addr;
        
    };
    [self.navigationController pushViewController:th animated:YES];
}
- (void)BarQu{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
